package itstudio.instructor.ui;

import itstudio.app.wq.R;
import itstudio.instructor.adapter.MyPostAdapter;
import itstudio.instructor.entity.News;
import itstudio.instructor.xlistview.XListView;
import itstudio.instructor.xlistview.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.nhaarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class MyPostActivity extends Activity implements IXListViewListener {
	private XListView my_post_listview;
	private View my_post_back_view;
	private MyPostAdapter adpater;
	private List<News> newsList;
	DisplayImageOptions options;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_post);
		initImageOptions();
		findById();
		setOnclick();
		initView();
	}

	private void initImageOptions() {
		options = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在内存中
				.showImageOnLoading(R.drawable.user_image_load_loading)
				.showImageOnFail(R.drawable.user_image_load_fail)
				.cacheOnDisc(true).considerExifParams(true)
				// 设置下载的图片是否缓存在内存中
				.displayer(new FadeInBitmapDisplayer(200))
				.bitmapConfig(Bitmap.Config.ALPHA_8).build(); // 设置图片的解码类型
	}

	private void findById() {
		// TODO Auto-generated method stub
		my_post_listview = (XListView) findViewById(R.id.my_post_listview);
		my_post_back_view = findViewById(R.id.my_post_back_layout);

	}

	private void initView() {
		// TODO Auto-generated method stub
		
		
		newsList = new ArrayList<News>();
		News news;

		news = new News();
		news.setTitle("头条新闻");
		news.setContent("客户端发布啦");
		news.setDate("2014-08-20");
		newsList.add(news);
		
		news = new News();
		news.setTitle("头条新闻");
		news.setContent("客户端发布啦");
		news.setDate("2014-08-20");
		newsList.add(news);
		
		news = new News();
		news.setTitle("头条新闻");
		news.setContent("客户端发布啦");
		news.setDate("2014-08-20");
		newsList.add(news);
		
		news = new News();
		news.setTitle("头条新闻");
		news.setContent("客户端发布啦");
		news.setDate("2014-08-20");
		newsList.add(news);
		
		news = new News();
		news.setTitle("头条新闻");
		news.setContent("客户端发布啦");
		news.setDate("2014-08-20");
		newsList.add(news);
		
		news = new News();
		news.setTitle("头条新闻");
		news.setContent("客户端发布啦");
		news.setDate("2014-08-20");
		newsList.add(news);
		
		news = new News();
		news.setTitle("头条新闻");
		news.setContent("客户端发布啦");
		news.setDate("2014-08-20");
		newsList.add(news);


		// 加载延迟
		adpater = new MyPostAdapter(MyPostActivity.this,
				newsList, options);
		AnimationAdapter animAdapter = new SwingBottomInAnimationAdapter(
				adpater);
		animAdapter.setAbsListView(my_post_listview);
		animAdapter.setInitialDelayMillis(300);
		
		my_post_listview.setPullLoadEnable(true);
		my_post_listview.setPullRefreshEnable(false);// 取消下拉刷新

		my_post_listview.setAdapter(animAdapter);
		my_post_listview.setXListViewListener(this);
		my_post_listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				TextView my_post_list_item_title=(TextView) arg1.findViewById(R.id.my_post_list_item_title);
				my_post_list_item_title.setTextColor(getResources().getColor(R.color.light_black));
				intent.setClass(getApplicationContext(),
						NewsDetailActivity.class);
				startActivity(intent);
			}
		});

	}

	private void setOnclick() {
		// TODO Auto-generated method stub
		MyOnClick onClick = new MyOnClick();
		my_post_back_view.setOnClickListener(onClick);

	}

	private class MyOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.my_post_back_layout:
				finish();
				break;

			default:
				break;
			}

		}

	}

	// 设置下拉刷新时间
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Handler mHandler = new Handler();
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				onLoad();
				// mAdapter.notifyDataSetChanged();
			}
		}, 2000);

	}

	// 设置loadmore刷新时间
	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Handler mHandler = new Handler();
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				onLoad();
			}
		}, 2000);

	}

	private void onLoad() {
		my_post_listview.stopRefresh();
		my_post_listview.stopLoadMore();
		my_post_listview.setRefreshTime("刚刚");
	}

}
